package com.changjiang.luban.admin;


import com.changjiang.luban.dao.domain.basic.ActivityBanner;
import com.changjiang.luban.service.basic.ActivityBannerServiceI;
import com.changjiang.luban.utils.ContrastUtils;
import com.changjiang.luban.utils.DateUtils;
import com.changjiang.luban.utils.data.Page;
import com.changjiang.luban.utils.file.FileUtils;
import com.changjiang.luban.utils.oss.CosUtils;
import com.changjiang.luban.utils.result.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

/**
 * basicController
 * Description:
 *
 * @author tengchenglong
 * @version v0.1
 * @date 2018/6/14-14:09
 */
@SuppressWarnings("ALL")
@Controller
@RequestMapping(value = "/admin/basic" , name = "基础设置")
public class BasicController {

    @Autowired
    private ActivityBannerServiceI bannerServiceI;



    /**
     * Api数据返回封装对象
     */
    private ApiResult apiResult = ApiResult.success();

    /**
     * 活动信息页面
     * @return
     */
    @GetMapping("/banner")
    public String banner(){
        return "basic/banner";
    }

    /**
     * 添加活动信息页面
     * @return
     */
    @GetMapping("/banner/add")
    public String bannerAdd(){
        return "basic/banner_add";
    }

    /**
     * 修改活动信息页面
     * @return
     */
    @GetMapping("/banner/edit/{id}")
    public String bannerEdit(@PathVariable Long id,HttpServletRequest request){
        ActivityBanner banner = bannerServiceI.findById(id);
        request.setAttribute("banner",banner);
        return "basic/banner_edit";
    }

    /**
     * 分页查询活动信息
     * @param search  查询参数
     * @param index   页码
     * @return
     */
    @PostMapping("/bannerList")
    @ResponseBody
    public ApiResult bannerList(String search,Integer index){
        Page<ActivityBanner> page = new Page<>();
        page.setIndex(index);
        page = bannerServiceI.pageByTitle(search,page);
        page.getPageBar();
        return ApiResult.success(page);
    }

    /**
     * 添加活动信息
     * @param file    图片
     * @param banner  活动实体
     * @param bindingResult
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/banner/save" , name = "添加新的banner")
    @ResponseBody
    public ApiResult saveBanner(@RequestParam("customFile") MultipartFile file,
                                @Valid ActivityBanner banner, BindingResult bindingResult) throws IOException {
        if (ContrastUtils.isNull(file.getOriginalFilename())) {
            File file1 =  FileUtils.convert(file);
            banner.setImgUrl(CosUtils.uploadFile(CosUtils.POSTER_BUCKET_NAME,file1));
            banner.setCreateTime(DateUtils.getCurrentSystemDate(DateUtils.DATE_FORMAT_COMPLETE));
            FileUtils.delete(file1.getName());
        }
        apiResult = ApiResult.success(bannerServiceI.saveBanner(banner));
        apiResult.setCallback("/admin/basic/banner");
        return apiResult;
    }

    /**
     * 修改banner信息
     * @param file    图片
     * @param banner  活动实体
     * @param bindingResult
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/update/banner" , name = "修改banner信息")
    @ResponseBody
    public ApiResult updadeBanner(@RequestParam("customFile") MultipartFile file,
                                @Valid ActivityBanner banner, BindingResult bindingResult) throws IOException {
        if (ContrastUtils.isNull(file.getOriginalFilename())) {
            File file1 =  FileUtils.convert(file);
            banner.setImgUrl(CosUtils.uploadFile(CosUtils.POSTER_BUCKET_NAME,file1));
            FileUtils.delete(file1.getName());
        } else {
          ActivityBanner banner1 = bannerServiceI.findById(banner.getId());
          banner.setImgUrl(banner1.getImgUrl());
        }
        apiResult = ApiResult.success(bannerServiceI.updateBanner(banner));
        apiResult.setCallback("/admin/basic/banner");
        return apiResult;
    }

    /**
     * 修改活动信息状态
     * @param id   活动编号
     * @param state 状态
     * @return
     */
    @PostMapping(value = "/banner/update", name = "修改banner状态")
    @ResponseBody
    public ApiResult updateBannerState(Long id, Integer state) {
        apiResult = ApiResult.success(bannerServiceI.updateState(id, state));
        apiResult.setCallback("/admin/basic/banner");
        return apiResult;
    }

    @PostMapping(value = "/banner/delete",name = "删除banner")
    @ResponseBody
    public ApiResult deleteBanner(Long id){
        apiResult =ApiResult.success(bannerServiceI.delete(id));
        apiResult.setCallback("/admin/basic/banner");
        return apiResult;
    }



}
